package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.query.NewsQueryObject;
import cn.wolfcode.trip.base.service.INewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 不出bug- 么么哒
 * @DESCRIPTION
 * @create 2018/11/13
 */
@RestController
@RequestMapping("/newses")
public class NewsController {
    @Autowired
    private INewsService newsService;

    /**
     * 根据新闻id查询新闻内容
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Object get(@PathVariable Long id) {
        return newsService.getNewsById (id);
    }

    /**
     * 获取带分页的新闻
     *
     * @param qo
     * @return
     */
    @GetMapping
    public Object query(@ModelAttribute("qo") NewsQueryObject qo) {
        qo.setOrderBy ("newsTime desc");
        qo.setPageSize (10);
        return newsService.query (qo);
    }

    /**
     * 查询相关推荐的新闻
     * 不查本文章
     *
     * @param type
     * @return
     */
    @GetMapping("/{type}/recommends")
    public Object getRecommends(@PathVariable Integer type, Long newsId) {
        return newsService.getRecommendNewesByType (type, newsId);
    }

    /**
     * 收藏资讯的关系关联
     */
    @PostMapping("/{newsId}/collections")
    public void insertRelations(@PathVariable Long newsId, Long userId) {
        newsService.insertRelation (newsId, userId);
    }

    /**
     * 取消收藏资讯的关系关联
     */
    @DeleteMapping("/{newsId}/collections/{userId}")
    public void deleteRelation(@PathVariable Long newsId, @PathVariable Long userId) {
        newsService.deleteRelation (newsId, userId);
    }

    /**
     * 查询user的收藏
     */
    @GetMapping("/{userId}/collections")
    public Object getNews(@PathVariable Long userId) {
        return newsService.selectByUserId (userId);
    }
}
